using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_MonthCardCell : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_MonthCardCell
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExText textName { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnFunction { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textPrice { protected set; get; }
            public UnityEngine.GameObject lockedTextNode { protected set; get; }
            public UnityEngine.GameObject unLockedTextNode { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textUnLocked { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textName", out var __tbv0);
                this.textName = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnFunction", out var __tbv1);
                this.btnFunction = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textPrice", out var __tbv2);
                this.textPrice = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("lockedTextNode", out var __tbv3);
                this.lockedTextNode = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("unLockedTextNode", out var __tbv4);
                this.unLockedTextNode = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textUnLocked", out var __tbv5);
                this.textUnLocked = __tbv5;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify



        #region fields
        private CfgMallItem cfgMallItem = null;
        #endregion fields

        #region properties

        protected UIB_UIN_MonthCardCell ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_MonthCardCell();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.btnFunction.AddClickEvent(this.OnBtnFunction_Clicked);
        }

        protected override void OnShow()
        {
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {
            this.uiMsgDispatcher.AddListener<int>(MsgEventType.MallItemMonthCardChanged, this.OnMsgMonthCardChanged);
        }

        protected override void OnDispose()
        {

        }

        #region methonds

        private void OnMsgMonthCardChanged(int mallItemCid)
        {
            if (this.cfgMallItem != null && this.cfgMallItem.Id == mallItemCid)
            {
                this.RefreshUnlock();
            }
        }

        public void Show(CfgMallItem cfgMallItem)
        {
            this.Show();
            this.cfgMallItem = cfgMallItem;
            this.ui.textName.text = this.cfgMallItem.Name;
            this.ui.textPrice.text = $"{APIMall.RechargeTitle} {this.cfgMallItem.Amount}";
            this.RefreshUnlock();
        }

        public void RefreshUnlock()
        {
            int invalidTime = MallModule.Instance.GetMonthCardInvlidTime(this.cfgMallItem.Id);
            int nowSecond = GameServerModule.Instance.ServerTimestamp;
            bool isUnlock = invalidTime != 0 && nowSecond < invalidTime;
            this.ui.lockedTextNode.SetActive(!isUnlock);
            this.ui.unLockedTextNode.SetActive(isUnlock);
            if (isUnlock)
            {
                int nLeftTime = invalidTime - nowSecond;
                this.ui.textUnLocked.text = string.Format(APIMall.MonthCardUnLockDesc, nLeftTime / 86400);
            }
        }

        private void OnBtnFunction_Clicked()
        {
            this.OpenUI<UIBuyPanel>(UD_BuyPanel.Create(this.cfgMallItem.Id));
        }
        #endregion methonds
    }
}
